<?php declare(strict_types=1);

namespace App\Aspect;

use App\Exception\ApiException;
use App\Library\Core\ErrCode;
use Swlib\Http\ContentType;
use Swoft\Aop\Annotation\Mapping\Aspect;
use Swoft\Aop\Annotation\Mapping\Before;
use Swoft\Aop\Annotation\Mapping\PointBean;
use Swoft\Aop\Annotation\Mapping\PointExecution;
use App\Library\Core\Token;
use Swoft\Redis\Redis;
use Swoft\Log\Helper\CLog;

/**
 * @Aspect(order=1)
 *
 * @PointExecution(
 *     include={"App\Http\Controller\Test\HomeController::call"}
 * )
 *
 * Class TestParamsAspect
 * @package App\Aspect
 */

class TestParamsAspect {
    //请求超时时间
    const VISIT_LIMIT_TIME = 300;

    //必填参数
    private $requiredParams = [
    ];

    //不参与sign的参数
    protected $notSignParams = [
    ];

    /**
     * @Before()
     */
    public function valid()
    {
        $request = context()->getRequest();
//        //校验请求头
        $auth = $request->getHeader('x-Authorization');
        $token = $auth ? $auth [0] : null;
        if (!$token) {
            throw new ApiException(ErrCode::msg(ErrCode::REQUEST_ILLEGAL) , ErrCode::REQUEST_ILLEGAL);
        }

        $params = $request->getQueryParams();

        //Token校验s
        //TODO: 调用user rpc服务，校验token
        $userInfo = Token::validateToken($token);
        if(!$userInfo)
        {
            throw new ApiException(ErrCode::msg(ErrCode::TOKEN_FAIL) , ErrCode::TOKEN_FAIL);
        }
        //写入上下文
        context()->set('userInfo' , $userInfo);
    }
}